/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//距下边界长度/单位px
var range = 50;
var page = 1;
var totalheight = 0;
//主体元素
var list = $("#list");
var loader = list.next();
var loading = false;

var loadContent = function (list, build = false) {
    if (loading)
        return;
    loading = true;
    $.ajax({
        type: "GET",
        url: list.data('href'),
        dataType: "html",
        data: {page: page},
        beforeSend: function () {
            //请求前的处理
            loader.show();
        },
        success: function (data) {
            if (data) {
                list.append(data);
                page++;
            } else {
                if (list.children().length) {
                    $('.mask-line').show();
                } else {
                    $('#empty').show();
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var msg = 'status:' + XMLHttpRequest.status;
            msg += ' readyState:' + XMLHttpRequest.readyState;
            msg += ' textStatus:' + textStatus;
        },
        complete: function () {
            //请求完成的处理
            loader.hide();
            loading = false;
        }
    });
};

$(window).scroll(function () {
    //滚动条距顶部距离(页面超出窗口的高度)
    var srollPos = $(window).scrollTop();
    totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
    if (($(document).height() - range) <= totalheight) {
        loadContent(list);
    }
});

loadContent(list);

